import { defineStore } from 'pinia'


type User = {
  name: string,
  age: number
}

export const useTestStore = defineStore('test', {
  state: () => {
    return {
      name: '孙小双',
      code: 'sunxiaoshuang',
      user: <User>{}
    }
  },
  getters: {
    newName(): string {
      return `getters: ${this.name}, ${this.code}`
    }
  },
  actions: {
    setUser() {
      this.user = {
        name: '孙远杰', age: 22
      }
    },
    async setUserAsync(): Promise<User> {
      return new Promise((resolve) => {
        setTimeout(() => {
          this.user = {
            name: '关羽', age: 2354
          }
        }, 2000);
      })
    }
  }
})